﻿using AutoMapper;
using Modules;
using Modules.Common;
using Modules.Project;
using PmSoft;
using Modules.System.User;
using PmSoft.ApplicationContext;
using PmSoft.Entity;

namespace ManagementApi.Models.Project;

/// <summary>
/// 保证金账户Dto
/// </summary>
public class ProjectGuaranteeExpenditureDto : OperateDto
{
    /// <summary>
    ///
    ///</summary>
    public int Id { get; set; }

    /// <summary>
    /// 项目Id
    ///</summary>
    public int? ProjectId { get; set; }

    /// <summary>
    /// 支出编号
    ///</summary>
    public string? Code { get; set; }

    /// <summary>
    /// 保证金缴纳信息Id
    ///</summary>
    public int? GuaranteePaymentId { get; set; }

    /// <summary>
    /// 动用通知书文号
    ///</summary>
    public string? NotificationLetter { get; set; }

    /// <summary>
    /// 保函（险）单号
    ///</summary>
    public string? GuaranteeLetterNo { get; set; }

    /// <summary>
    /// 收支金额
    ///</summary>
    public decimal? Amount { get; set; }

    /// <summary>
    /// 收支日期
    ///</summary>
    public DateTime? PayDate { get; set; }

    /// <summary>
    /// 创建人
    ///</summary>
    public string? Creator { get; set; }

    /// <summary>
    /// 创建时间
    ///</summary>
    public DateTime? CreateTime { get; set; }

    /************* 额外显示字段 ****************/

    /// <summary>
    /// 项目名称
    /// </summary>
    public string? ProjectName { get; set; }

    /// <summary>
    /// 项目编号
    /// </summary>
    public string? ProjectNum { get; set; }

    /// <summary>
    /// 保证金方式
    /// </summary>
    public string? GuaranteeAccountTypeName { get; set; }

    /// <summary>
    /// 缴纳主体名称
    /// </summary>
    public string? PaymentName { get; set; }

    /// <summary>
    /// 缴纳主体社会统一信用代码
    /// </summary>
    public string? PaymentCode { get; set; }
}

/// <summary>
/// 保证金扩展
/// </summary>
public static class ProjectGuaranteeExpenditureExceptions
{
    /// <summary>
    /// 返回前端显示数据
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static ProjectGuaranteeExpenditureDto AsDto(this ProjectGuaranteeExpenditure entity, IApplicationContext applicationContext)
    {
        ProjectGuaranteeExpenditureDto projectGuaranteeExpenditureDto = new();
        var dto = applicationContext.GetService<IMapper>().Map(entity, projectGuaranteeExpenditureDto);
        var projectService = applicationContext.GetService<ProjectService>();
        var project = projectService.GetProjectInfo(entity.ProjectId);
        if (project != null)
        {
            #region 主管单位数据权限

            if (applicationContext.CurrentUser is IDeptUser deptUser)
            {
                if (deptUser.IsSuperAdministrator() || deptUser.IsRSAdministrator() || project.DeptId == deptUser.DeptId)
                    dto.HasEdit = true;
                else dto.HasEdit = false;
            }

            #endregion 主管单位数据权限

            dto.ProjectName = project.Name;
            dto.ProjectNum = project.PrjNum;
        }
        var projectGuaranteePaymentService = applicationContext.GetService<ProjectGuaranteePaymentService>();

        var projectGuaranteePayment = projectGuaranteePaymentService.GetProjectGuaranteePayment(entity.GuaranteePaymentId ?? 0);
        if (projectGuaranteePayment != null)
        {
            var projectGuaranteeAccountService = applicationContext.GetService<ProjectGuaranteeAccountService>();
            var projectGuaranteeAccount = projectGuaranteeAccountService.GetProjectGuaranteeAccount(projectGuaranteePayment.GuaranteeAccountId);
            dto.GuaranteeAccountTypeName = projectGuaranteeAccount?.Type.GetDescription() ?? string.Empty;
            dto.PaymentName = projectGuaranteePayment?.PaymentName;
            dto.PaymentCode = projectGuaranteePayment?.PaymentCode;
        }
        return dto;
    }
}